package com.sirdc.modules.sys.web.Accraditation;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.Project.util.MessageUtil;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.filter.NodeNewschedulesFilter;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.node.utile.DateUtil;
import net.huashitong.oversee.JobTask.JobTaskService;
import net.huashitong.oversee.entity.SupervisionTask;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.FeedBackLeaderService;
import net.huashitong.oversee.service.ScoreSummaryService;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.entity.Message;
import net.huashitong.user.service.BnpUserService;
import net.huashitong.user.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static net.huashitong.Project.util.HolidayUtils.calLeaveDays;

@Controller
@RequestMapping("/accAudit")
public class AccraditationAuditController {
    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;
    @Autowired
    @Lazy
    private NewProjectsService newProjectsService;
    @Autowired
    private MessageService ms;
    @Autowired
    private SupervisionTaskService supervisionTaskService;
    @Autowired
    private MessageUtil mu;
    @Autowired
    private FeedBackLeaderService feedService;
    @Autowired
    private JobTaskService jobTaskService;
    @Autowired
    private ScoreSummaryService scoreSummaryService;
    @Autowired
    private BnpUserService bnpUserService;

    @ModelAttribute("headType")
    public String returnHeadType() {
        return "节点审核模块";
    }

    @RequestMapping(value = "/go/list")
    public String accAudit(HttpServletRequest request, Model model) {
        model.addAttribute("leftType", "待办任务");
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        NodeNewschedulesFilter filter = new NodeNewschedulesFilter();
        if (ObjectUtils.isBlank(bnpUser)) {
            return "/modules/login/login";
        }
        filter.setOperatorUser(bnpUser.getName());
        filter.setOperatorPhone(bnpUser.getPhone());
        List<Map<String, Object>> examineMap = nodeNewschedulesService.getExamineList(filter);
        double totalPage = (double) nodeNewschedulesService.queryListByFilterZong(filter).size();
        totalPage = Math.ceil(totalPage / 10);
        model.addAttribute("totalPage", totalPage);

        //待办任务个数
        int nodeCount = (int) WebUtils.getSessionAttribute(request, "nodeCount");
        model.addAttribute("nodeCount", nodeCount);

        int myTaskNum = (int) WebUtils.getSessionAttribute(request, "myTaskNum");
        int quetionNum = (int) WebUtils.getSessionAttribute(request, "quetionNum");
        model.addAttribute("myTaskNum", myTaskNum);
        model.addAttribute("quetionNum", quetionNum);

        int myTackTaskNum = (int) WebUtils.getSessionAttribute(request, "myTackTaskNum");
        model.addAttribute("myTackTaskNum", myTackTaskNum);

        return "/modules/Accraditation/Accraditation_sh_node";
    }

    @ResponseBody
    @RequestMapping(value = "/go/lists", method = RequestMethod.POST)
    public Object goList(@ModelAttribute NodeNewschedulesFilter filter, HttpServletRequest request) {
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        List<Map<String, Object>> list = new ArrayList<>();
        if (ObjectUtils.isBlank(bnpUser)) {
            return "/modules/login/login";
        }
        filter.setOperatorPhone(bnpUser.getPhone());
        List<NodeNewschedules> lists = nodeNewschedulesService.queryListByFilterList(filter);
        if (ObjectUtils.isNotBlank(lists)) {
            for (NodeNewschedules nns : lists) {
                Map<String, Object> maps = new HashMap<>();
                NewProjects np = newProjectsService.getById(nns.getProjectId());
                maps.put("projectsName", np.getName());
                maps.put("sysId", nns.getSysId());
                maps.put("nodeId", nns.getNodeId());
                maps.put("nodeName", nns.getNodeName());
                maps.put("planBeginDate", nns.getPlanBeginDate());
                maps.put("planTime", nns.getPlanTime());
                maps.put("status", nns.getStatus());
                list.add(maps);
            }
        }

        double totalPage = (double) nodeNewschedulesService.queryListByFilterZong(filter).size();
        totalPage = Math.ceil(totalPage / 10);
        System.out.println("totalPage" + totalPage);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("totalPage", totalPage);
        return map;
    }


    /**
     * 部门确定节点（）
     *
     * @param id
     * @return
     */
    @RequestMapping("/accAgree")
    public String accAgree(@RequestParam("nodeId") String id) {
        NodeNewschedules nns = nodeNewschedulesService.getById(id);
        nns.setDenyReason(null);
        nns.setStatus("2");
        nns.setFactFinishDate(DateUtil.getDate());
        nodeNewschedulesService.update(nns);
        List<NodeNewschedules> lists = nodeNewschedulesService.getListNodeIds(nns.getNodeIds(), nns.getProjectId());
        List<NodeNewschedules> list0 = nodeNewschedulesService.getNodeIdsList(nns.getNodeIds(), nns.getProjectId(), "");
        //判断所在节点小节点是否全部完成，
        //完成---大节点改为2-----
        if (list0.size() - lists.size() == 0) {
            NodeNewschedules bigNns = nodeNewschedulesService.getNnsByProjectIdAndNodeId(nns.getProjectId(), nns.getNodeIds());
            bigNns.setStatus("2");
            bigNns.setFactFinishDate(DateUtil.getDate());
            nodeNewschedulesService.update(bigNns);
            //根据前置
            List<NodeNewschedules> lists1 = nodeNewschedulesService.getListByPreNodeId(bigNns.getProjectId(), bigNns.getNodeId());
            if (ObjectUtils.isNotBlank(lists1)) {
                for (NodeNewschedules nnss : lists1) {
                    nnss = nodeNewschedulesService.getById(nnss.getSysId());
                    List<NodeNewschedules> lists2 = nodeNewschedulesService.getListByPreNodeId2(nnss.getPreNodeId(), nnss.getProjectId());
                    if (ObjectUtils.isNotBlank(lists2)) {
                        int ss = 0;
                        for (NodeNewschedules nnss2 : lists2) {
                            if ("2".equals(nnss2.getStatus())) {
                                ss++;
                            } else {
                                break;
                            }
                        }
                        if (lists2.size() == ss) {
                            nnss.setStatus("1");
                            nnss.setFactFinishDate(DateUtil.getDate());
                            nodeNewschedulesService.update(nnss);
                            NodeNewschedules smallNns = nodeNewschedulesService.getNnsByProjectIdAndNodeId(nnss.getProjectId(), nnss.getNodeId() + ".1");
                            if (ObjectUtils.isNotBlank(smallNns)) {
                                smallNns.setStatus("1");
                                smallNns.setPlanBeginDate(DateUtil.getDate());
                                nodeNewschedulesService.update(smallNns);
                                NewProjects np = newProjectsService.getById(nns.getProjectId());
                                Map<String, Object> ma = mu.taskMessage(smallNns.getOperatorUser(), smallNns.getOperatorPhone(), "《" + np.getName() + "》", smallNns.getNodeId());
                                if ("0".equals(ma.get("code").toString())) {
                                    Message m = new Message();
                                    m.setRecipients(smallNns.getOperatorUser());
                                    m.setText(ma.get("text").toString());
                                    m.setType("1");
//                                    测试用
                                    m.setPhone(smallNns.getOperatorPhone());
                                    m.setMsg(ma.get("msg").toString());
                                    m.setProjectName(np.getName());
                                    m.setResDept(smallNns.getResDept());
                                    m.setNodeId(nns.getSysId());
                                    ms.save(m);
                                }
                            }
                        }
                    }
                }
            }
        } else {
            //下一个小节点开始操作
            int nodeId = Integer.parseInt(nns.getNodeId().substring(nns.getNodeId().length() - 1, nns.getNodeId().length())) + 1;
            NodeNewschedules smallNns = nodeNewschedulesService.getNnsByProjectIdAndNodeId(nns.getProjectId(), nns.getNodeIds() + "." + nodeId);
            if (ObjectUtils.isNotBlank(smallNns)) {
                smallNns.setStatus("1");
                smallNns.setPlanBeginDate(DateUtil.getDate());
                nodeNewschedulesService.update(smallNns);
                NewProjects np = newProjectsService.getById(nns.getProjectId());
                Map<String, Object> ma = mu.taskMessage(smallNns.getOperatorUser(), smallNns.getOperatorPhone(), "《" + np.getName() + "》", smallNns.getNodeId());
                if ("0".equals(ma.get("code").toString())) {
                    Message m = new Message();
                    m.setRecipients(smallNns.getOperatorUser());
                    m.setText(ma.get("text").toString());
                    m.setType("1");
                    m.setPhone(smallNns.getOperatorPhone());
                    m.setMsg(ma.get("msg").toString());
                    m.setProjectName(np.getName());
                    m.setResDept(smallNns.getResDept());
                    m.setNodeId(np.getSysId());
                    ms.save(m);
                }
            }
        }
        return "redirect:/accAudit/go/list";
    }

    /**
     * 部门回退节点
     *
     * @param id
     * @param denyReason
     * @return
     */
    @RequestMapping(value = "/accDeny", method = RequestMethod.POST)
    public String accDeny(@RequestParam("nodeId") String id, @RequestParam(value = "denyReason", required = false) String denyReason) {
        NodeNewschedules nns = nodeNewschedulesService.getById(id);
        nns.setStatus("0");
        nns.setDenyReason(denyReason);
        nodeNewschedulesService.update(nns);
        List<NodeNewschedules> nodesBCN = nodeNewschedulesService.getNodesBeforeCurrentNode(nns.getNodeIds(), nns.getProjectId(), nns.getNodeId());
        int nodeIdIndex = nodesBCN.size();
        //审批节点的上一个节点状态改成进行中--------nodeIdIndex-2
        NodeNewschedules previousNode = nodeNewschedulesService.getById(nodesBCN.get(nodeIdIndex - 2).getSysId());
        //上一个节点设置进行中
        previousNode.setStatus("1");
        previousNode.setFactTime("0");
        previousNode.setFactFinishDate(null);
        previousNode.setDenyReason(denyReason);
        previousNode.setPlanBeginDate(DateUtil.getDate());
        nodeNewschedulesService.update(previousNode);
        //发短信通知节点被回退
        NewProjects project = newProjectsService.getById(nns.getProjectId());
        //节点被审核部门回退，需要发短信通知业主经办人
        Map<String, Object> ma = mu.taskBackMessage(previousNode.getOperatorUser(), previousNode.getOperatorPhone(), "《" + project.getName() + "》", previousNode.getNodeId());
        if ("0".equals(ma.get("code").toString())) {
            Message m = new Message();
            m.setRecipients(previousNode.getOperatorUser());
            m.setText(ma.get("text").toString());
            m.setType("1");
            m.setPhone(previousNode.getOperatorPhone());
            m.setMsg(ma.get("msg").toString());
            m.setProjectName(project.getName());
            m.setResDept(previousNode.getResDept());
            m.setNodeId(previousNode.getSysId());
            ms.save(m);
        }


        return "redirect:/accAudit/go/list";
    }

    //获取业主的备件信息
    @ResponseBody
    @RequestMapping(value = "/getNodeNewschedulesById", method = RequestMethod.POST)
    public Object getNodeNewschedulesById(@RequestBody NodeNewschedulesFilter filter) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        NodeNewschedules nns = nodeNewschedulesService.getById(filter.getSysId());
        List<NodeNewschedules> lists = nodeNewschedulesService.getNodeIdsList(nns.getNodeIds(), nns.getProjectId(), "");
        for (NodeNewschedules nnss : lists) {
            if (!filter.getSysId().equals(nnss.getSysId()) && !"2".equals(nnss.getState()) && "2".equals(nnss.getStatus())) {
                Map<String, Object> maps = new HashMap<>();
                maps.put("sysId", nnss.getSysId());
                maps.put("nodeId", nnss.getNodeId());
                maps.put("nodeName", nnss.getNodeName());
                maps.put("nodeFilter", nnss.getNodeFilter());
                list.add(maps);
            }

        }
        map.put("list", list);
        return map;
    }


    /**
     * 部门单位，节点最后审核是否通过（比较重要的一个接口）
     *
     * @param filter
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/jointProcessing", method = RequestMethod.POST)
    public Object jointProcessing(@RequestBody NodeNewschedulesFilter filter, Model model, HttpServletRequest request) throws ParseException {

        NodeNewschedules nns = nodeNewschedulesService.getById(filter.getSysId());
        NewProjects project = newProjectsService.getById(nns.getProjectId());
//        int factTime0 = betweenTwoDateNum(nns.getPlanBeginDate(), DateUtil.getDate()) + 1;

        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy.MM.dd");
        Date planBeginDate = myFmt.parse(nns.getPlanBeginDate());
        String factFinishDate = DateUtil.getDate();
        Date finishDate = myFmt.parse(factFinishDate);
        //实际完成的天数(过滤周末)
        double factTime0 = calLeaveDays(planBeginDate, finishDate);

        //审批部门操作完成，如果改节点处于督查，则督查任务自动完成
        SupervisionTaskFilter taskFilter = new SupervisionTaskFilter();
        taskFilter.setProjectId(nns.getProjectId());
        taskFilter.setNodeId(nns.getNodeId());
        List<SupervisionTask> t = supervisionTaskService.getTaskListsNoPage(taskFilter);
        for (SupervisionTask task : t) {
            task.setRespResult("1");
            //发短信给相关督查组，提醒办结任务
            mu.remindToDoTaskToSupervisionMessage(task);
        }
        supervisionTaskService.batchUpdate(t);

        Map<String, Object> map = new HashMap<>();
        if ("1".equals(filter.getStatus())) {//本节点操作成功

            String planTime = nns.getPlanTime();
            int planTimeInt = Integer.parseInt(StringUtils.isBlank(planTime) ? "1" : planTime);
            //如果计划用时大于等于实际用时，则正常完成该节点（绩效得100分）
            if (Double.parseDouble(planTimeInt + "") >= factTime0) {
                newProjectsService.updateFullMarkPoint(nns, project);
            }

            //更新本节点数据信息
            nns.setStatus("2");
            nns.setFactTime((int) factTime0 + "");
            nns.setFactFinishDate(DateUtil.getDate());
            nodeNewschedulesService.update(nns);
            //判断是否为本大节点的最后一个节点
            //已完成的小节点-lists
            List<NodeNewschedules> lists = nodeNewschedulesService.getListNodeIds(nns.getNodeIds(), nns.getProjectId());
            List<NodeNewschedules> allSmallNodes = nodeNewschedulesService.getNodeIdsList(nns.getNodeIds(), nns.getProjectId(), "1");
            if (allSmallNodes.size() - lists.size() == 0) {
                //更新当前大节点的信息
                NodeNewschedules bigNns = nodeNewschedulesService.getNnsByProjectIdAndNodeId(nns.getProjectId(), nns.getNodeIds());
                bigNns.setStatus("2");  //设置大节点的状态为完成
                int factTime = 0;
                for (NodeNewschedules n : lists) {
                    factTime = Integer.parseInt(n.getFactTime()) + factTime;
                }
                bigNns.setFactTime(factTime + "");
                bigNns.setFactFinishDate(DateUtil.getDate());
                nodeNewschedulesService.update(bigNns);
                //根据前置字段启动下个一个大节点开始（获取下个大节点的id）
                List<NodeNewschedules> lists1 = nodeNewschedulesService.getListByPreNodeId(bigNns.getProjectId(), bigNns.getNodeId());
                if (ObjectUtils.isNotBlank(lists1)) {
                    for (NodeNewschedules nnss : lists1) {
                        nnss = nodeNewschedulesService.getById(nnss.getSysId());
                        //lists2结果为上一个节点的id
                        List<NodeNewschedules> lists2 = nodeNewschedulesService.getListByPreNodeId2(nnss.getPreNodeId(), nnss.getProjectId());
                        if (ObjectUtils.isNotBlank(lists2)) {
                            int ss = 0;
                            for (NodeNewschedules nnss2 : lists2) {
                                if ("2".equals(nnss2.getStatus())) {
                                    ss++;
                                } else {
                                    break;
                                }
                            }
                            //说明以上的节点已经完成
                            if (lists2.size() == ss) {
                                //如果下个大节点不需要，则跳过
                                if ("2".equals(nnss.getState())) {
                                    List<NodeNewschedules> nextNodeList = nodeNewschedulesService.getListByPreNodeId(nnss.getProjectId(), nnss.getNodeId());
                                    if (ObjectUtils.isNotBlank(nextNodeList)) {
                                        for (NodeNewschedules next : nextNodeList) {
                                            //大节点
                                            NodeNewschedules nodeNewschedules = nodeNewschedulesService.getById(next.getSysId());
                                            nodeNewschedules.setState("1");
                                            nodeNewschedules.setPlanBeginDate(DateUtil.getDate());
                                            nodeNewschedulesService.update(nodeNewschedules);
                                            List<NodeNewschedules> newschedules = nodeNewschedulesService.getNodeIds(nodeNewschedules.getNodeId(), nodeNewschedules.getProjectId());
                                            if (ObjectUtils.isNotBlank(newschedules)) {
                                                NodeNewschedules smallNns = newschedules.get(0);
                                                smallNns.setStatus("1");
                                                smallNns.setPlanBeginDate(DateUtil.getDate());
                                                nodeNewschedulesService.update(smallNns);
                                                NewProjects np = newProjectsService.getById(nns.getProjectId());
                                                Map<String, Object> ma = mu.taskMessage(smallNns.getOperatorUser(), smallNns.getOperatorPhone(), "《" + np.getName() + "》", smallNns.getNodeId());
                                                if ("0".equals(ma.get("code").toString())) {
                                                    Message m = new Message();
                                                    m.setRecipients(smallNns.getOperatorUser());
                                                    m.setText(ma.get("text").toString());
                                                    m.setType("1");
                                                    m.setPhone(smallNns.getOperatorPhone());
                                                    m.setMsg(ma.get("msg").toString());
                                                    m.setProjectName(np.getName());
                                                    m.setResDept(smallNns.getResDept());
                                                    m.setNodeId(nns.getSysId());
                                                    ms.save(m);
                                                }
                                            } else {
                                                //如果找不到下个节点则开启确定的大节点与第一个小节点
                                                NewProjects np = newProjectsService.getById(nns.getProjectId());
                                                //获取确认需要的大节点
                                                List<NodeNewschedules> nodeList = nodeNewschedulesService.getNeedBigNodeByProjectId(np.getName());
                                                if (nodeList.size() > 0) {
                                                    NodeNewschedules newBigNode = nodeList.get(0);
                                                    newBigNode.setStatus("1");
                                                    newBigNode.setPlanBeginDate(DateUtil.getDate());
                                                    nodeNewschedulesService.update(newBigNode);
                                                    //获取确认需要的小节点
                                                    List<NodeNewschedules> smallNodeList = nodeNewschedulesService.getNodeIds(newBigNode.getNodeId(), np.getSysId());
                                                    if (smallNodeList.size() > 0) {
                                                        NodeNewschedules smallNns = smallNodeList.get(0);
                                                        smallNns.setStatus("1");
                                                        smallNns.setPlanBeginDate(DateUtil.getDate());
                                                        nodeNewschedulesService.update(smallNns);
                                                        Map<String, Object> ma = mu.taskMessage(smallNns.getOperatorUser(), smallNns.getOperatorPhone(), "《" + np.getName() + "》", smallNns.getNodeId());
                                                        if ("0".equals(ma.get("code").toString())) {
                                                            Message m = new Message();
                                                            m.setRecipients(smallNns.getOperatorUser());
                                                            m.setText(ma.get("text").toString());
                                                            m.setType("1");
                                                            m.setPhone(smallNns.getOperatorPhone());
                                                            m.setMsg(ma.get("msg").toString());
                                                            m.setProjectName(np.getName());
                                                            m.setResDept(smallNns.getResDept());
                                                            m.setNodeId(np.getSysId());
                                                            ms.save(m);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    //下个节点未完成，判断它的所有前置节点是否完成，如果完成则开启，否则不开启
                                    String preNodeStr = nnss.getPreNodeId();
                                    if(preNodeStr.contains(",")){
                                        String[] preNode = preNodeStr.split(",");
                                        boolean flag = true;
                                        for(String p : preNode){
                                            NodeNewschedules bigNode = nodeNewschedulesService.getBigNodesState(nnss.getProjectId(), p);
                                            if(ObjectUtils.isNotBlank(bigNode)){
                                                if(!"2".equals(bigNode.getStatus())){
                                                    flag = false;
                                                }
                                            }
                                        }
                                        if(flag){
                                            nnss.setStatus("1");
                                            nnss.setPlanBeginDate(DateUtil.getDate());
                                            nodeNewschedulesService.update(nnss);
                                            List<NodeNewschedules> newschedules = nodeNewschedulesService.getNodeIds(nnss.getNodeId(), nnss.getProjectId());
                                            if (ObjectUtils.isNotBlank(newschedules)) {
                                                NodeNewschedules smallNns = newschedules.get(0);
                                                smallNns.setStatus("1");
                                                smallNns.setPlanBeginDate(DateUtil.getDate());
                                                nodeNewschedulesService.update(smallNns);
                                                NewProjects np = newProjectsService.getById(nns.getProjectId());
                                                Map<String, Object> ma = mu.taskMessage(smallNns.getOperatorUser(), smallNns.getOperatorPhone(), "《" + np.getName() + "》", smallNns.getNodeId());
                                                if ("0".equals(ma.get("code").toString())) {
                                                    Message m = new Message();
                                                    m.setRecipients(smallNns.getOperatorUser());
                                                    m.setText(ma.get("text").toString());
                                                    m.setType("1");
                                                    m.setPhone(smallNns.getOperatorPhone());
                                                    m.setMsg(ma.get("msg").toString());
                                                    m.setProjectName(np.getName());
                                                    m.setResDept(smallNns.getResDept());
                                                    m.setNodeId(nns.getSysId());
                                                    ms.save(m);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {//本大节点还有未完成节点
                //下一个小节点开始操作
                NodeNewschedules smallNns = null;
                for (NodeNewschedules allNode : allSmallNodes) {
                    if ("0".equals(allNode.getStatus())) {
                        smallNns = allNode;
                    }
                }
                if (ObjectUtils.isNotBlank(smallNns)) {
                    smallNns.setStatus("1");
                    smallNns.setPlanBeginDate(DateUtil.getDate());
                    nodeNewschedulesService.update(smallNns);
                    NewProjects np = newProjectsService.getById(nns.getProjectId());
                    Map<String, Object> ma = mu.taskMessage(smallNns.getOperatorUser(), smallNns.getOperatorPhone(), "《" + np.getName() + "》", smallNns.getNodeId());
                    if ("0".equals(ma.get("code").toString())) {
                        Message m = new Message();
                        m.setRecipients(smallNns.getOperatorUser());
                        m.setText(ma.get("text").toString());
                        m.setType("1");
                        m.setPhone(smallNns.getOperatorPhone());
                        m.setMsg(ma.get("msg").toString());
                        m.setProjectName(np.getName());
                        m.setResDept(smallNns.getResDept());
                        m.setNodeId(np.getSysId());
                        ms.save(m);
                    }
                } else {

                }
            }

        } else if ("0".equals(filter.getStatus())) {//退回操作
            nns.setStatus("0");
            nns.setPlanBeginDate("");           //部门开始时间重新计时
            nodeNewschedulesService.update(nns);

            List<NodeNewschedules> nodesBCN = nodeNewschedulesService.getNodesBeforeCurrentNode(nns.getNodeIds(), nns.getProjectId(), nns.getNodeId());
            int nodeIdIndex = nodesBCN.size();
            //审批节点的上一个节点状态改成进行中--------nodeIdIndex-2
            NodeNewschedules previousNode = nodeNewschedulesService.getById(nodesBCN.get(nodeIdIndex - 2).getSysId());

            //上一个节点设置进行中
            previousNode.setStatus("1");
            previousNode.setFactTime("0");
            previousNode.setFactFinishDate(null);
            previousNode.setDenyReason(filter.getDenyReason());
            previousNode.setPlanBeginDate(DateUtil.getDate());
            nodeNewschedulesService.update(previousNode);

            //节点被审核部门回退，需要发短信通知业主经办人
            Map<String, Object> ma = mu.taskBackMessage(previousNode.getOperatorUser(), previousNode.getOperatorPhone(), "《" + project.getName() + "》", previousNode.getNodeId());
            if ("0".equals(ma.get("code").toString())) {
                Message m = new Message();
                m.setRecipients(previousNode.getOperatorUser());
                m.setText(ma.get("text").toString());
                m.setType("1");
                m.setPhone(previousNode.getOperatorPhone());
                m.setMsg(ma.get("msg").toString());
                m.setProjectName(project.getName());
                m.setResDept(previousNode.getResDept());
                m.setNodeId(previousNode.getSysId());
                ms.save(m);
            }
            //督查任务重新开启设置（）
            SupervisionTaskFilter supervisionTaskFilter = new SupervisionTaskFilter();
            supervisionTaskFilter.setProjectId(previousNode.getProjectId());
            supervisionTaskFilter.setNodeId(previousNode.getNodeId());
            List<SupervisionTask> tasks = supervisionTaskService.querySupervisionTaskByFilter(supervisionTaskFilter);
            if (tasks.size() > 0) {
                //说明节点有进入督查任务
                SupervisionTask task = tasks.get(0);
                task.setRespResult("0");
                task.setSupervisionResult("0");
                task.setTaskResult("0");
                task.setAaa001("1");
                //督查组手机号
                String[] phones = task.getPhones().split(",");
                //督查组人员
                String[] inspector = task.getInspectors().split(",");
                String[] groupType = task.getGroupType().split(",");
                for (int i = 1; i < phones.length; i++) {
                    int codeducha = feedService.sendMsgToDuchaAndDept(task.getProjectName(),
                            task.getSysId(),
                            task.getTaskName(), phones[i]);
                    Message m = new Message();
                    m.setProjectName(task.getTaskName());
                    m.setResDept(groupType[i]);
                    m.setRecipients(inspector[i]);
                    m.setPhone(phones[i]);
                    m.setType("9");
                    m.setText("督查任务单单号:" + task.getSysId() + ",任务详情请到督查任务菜单查看!");
                    m.setBranchType("4");
                    ms.save(m);
                    if (0 == codeducha) {
                        task.setAaa002("1");
                        supervisionTaskService.update(task);
                    }
                }
                //经办人发短信
                int codejingban = feedService.sendMsgToDuchaAndDept(task.getProjectName(),
                        task.getSysId(),
                        task.getTaskName(),
                        task.getOperatorPhone()/*"18350300651"*/);
                Message m = new Message();
                m.setProjectName(task.getTaskName());
                m.setResDept(task.getResDept());
                m.setRecipients(task.getOperatorUser());
                m.setPhone(task.getOperatorPhone());
                m.setType("9");
                m.setText("督查任务单单号:" + task.getSysId() + ",任务详情请到督查任务菜单查看!");
                BnpUser user = bnpUserService.getBnpUserByPhoneAndName(task.getOperatorPhone(), task.getOperatorUser());
                if (ObjectUtils.isNotBlank(user)) {
                    m.setBranchType(user.getBranchType());
                }
                ms.save(m);
                //责任人
                int codezeren = feedService.sendMsgToDuchaAndDept(task.getProjectName(),
                        task.getSysId(),
                        task.getTaskName(),
                        task.getResPhone()/*"18259269331"*/);
                m = new Message();
                m.setResDept(task.getResDept());
                m.setProjectName(task.getTaskName());
                m.setRecipients(task.getResUser());
                m.setPhone(task.getResPhone());
                m.setType("9");
                m.setText("督查任务单单号:" + task.getSysId() + ",任务详情请到督查任务菜单查看!");
                if (ObjectUtils.isNotBlank(user)) {
                    m.setBranchType(user.getBranchType());
                }
                ms.save(m);


                //节点有进入督查任务要删掉相应的绩效还有更新汇总

                jobTaskService.saveSourceExpire();
                scoreSummaryService.updateSummaryScore();
                scoreSummaryService.updateSummaryScoreRank();
                String updateScore = supervisionTaskService.updateAllTaskRespAndSupervisionScore();
                if ("ok".equals(updateScore)) {
                    System.out.println("123");
                }
                scoreSummaryService.updateSummaryScoreTypeRank();


            }
//            GradePointFilter gradePointFilter = new GradePointFilter();
//            List<GradePoint> gradePoints = gradePointService.queryGradePoint(gradePointFilter);
        }

        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        NodeNewschedulesFilter nodeNewschedulesFilter = new NodeNewschedulesFilter();
        nodeNewschedulesFilter.setOperatorUser(bnpUser.getName());
        nodeNewschedulesFilter.setOperatorPhone(bnpUser.getPhone());
        //进行中的小节点个数
        double totalPage12 = (double) nodeNewschedulesService.queryListByFilterZong(nodeNewschedulesFilter).size();
        model.addAttribute("nodeCount", (new Double(totalPage12)).intValue());
        //保存更新nodeCount
        WebUtils.setSessionAttribute(request, "nodeCount", (new Double(totalPage12)).intValue());

        map.put("code", "200");
        return map;
    }


    @RequestMapping("/accDownload/{id}")
    public void accDownload(@PathVariable("id") String id) {

    }

}
